package com.fjnu.controller.borrow;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fjnu.controller.AdministratorController;
import com.fjnu.controller.common.BaseController;
import com.fjnu.entities.Borrow;
import com.fjnu.entities.MainInstrument;
import com.fjnu.entities.User;
import com.fjnu.service.IBorrowService;
import com.fjnu.service.InstrumentService;

@Controller
public class BorrowController extends BaseController{
	private static Logger logger =LogManager.getLogger(BorrowController.class);
	@Autowired
	private IBorrowService borrowService;
	@Autowired
	private InstrumentService instrumentService;
	/**
	 * 填写一份申请单
	 * @param mainId
	 * @param useReason
	 * @param returnTime
	 * @return
	 */
	@RequiresRoles("普通教职工")
	@RequestMapping(value="/addBorrow")
	@ResponseBody
	public Map<String,Object> addBorrow(Integer mainId,String useReason,Date returnTime){
		logger.info("申请借用单");
		if(!init(mainId,useReason).get(STATUS).equals(SUCCESS))
			return getData2Result();
			MainInstrument mainInstrument = instrumentService.findUniqueInstrument(mainId);
			if(mainInstrument.getStatusQuo().equals(mainInstrument.GOOD)){
			//mainInstrument.setStatusQuo(mainInstrument.BORROW);
			Subject currentUser = SecurityUtils.getSubject();  
			Session session = currentUser.getSession();
			User user=(User) session.getAttribute("subject");
			Date borrowTime = new Date();
			//Date returnTime1 =new Date();
			String status = "未审核";
			Borrow borrow =new Borrow(user,useReason,borrowTime,returnTime
					,mainInstrument,status);
			borrowService.addBorrow(borrow);
			}
			else{
			putFail("当前设备不可借");
			}
		return getData2Result();
	}
	/**
	 * 受理一份申请单
	 * @param id
	 * @param status 0：审核通过 1：审核不通过
	 * @return
	 */
	@RequestMapping(value="/answerBorrow")
	@ResponseBody
	@RequiresRoles("后勤管理员")
	public Map<String,Object> answerBorrow(Integer id,Integer status){
		logger.info("受理一份借用单");
		if(!init(id).get(STATUS).equals(SUCCESS))
			return getData2Result();
		Borrow borrow = borrowService.findUniqueBorrow(id);
		if(status==0){
			borrow.setStatus(borrow.PASSEXAM);
			borrow.setProcess(borrow.NORETURN);
			borrow.getMainInstrument().setStatusQuo("借出");
			borrow.getMainInstrument().setRecipientsPerson(borrow.getBorrowPerson().getName());
		}
		else{
			borrow.setStatus(borrow.NOPASS);
		}
		Subject currentUser = SecurityUtils.getSubject();  
		Session session = currentUser.getSession();
		User user=(User) session.getAttribute("subject");	
		borrow.setExamPerson(user);
		borrow.setExamTime(new Date());
		borrowService.updateBorrow(borrow);
		return getData2Result();
	}
	/**
	 * 查找当前用户所申请的借用单
	 * @return
	 */
	@RequiresRoles("普通教职工")
	@RequestMapping(value="/findBorrowByBUser")
	@ResponseBody
	public Map<String,Object> findBorrowByBUser(){
		logger.info("查找当前用户所申请的借用单");
		if(!init().get(STATUS).equals(SUCCESS))
			return getData2Result();
		Subject currentUser = SecurityUtils.getSubject();  
		Session session = currentUser.getSession();
		User borrowUser=(User) session.getAttribute("subject");
		List<Borrow> list = borrowService.findBorrowByBUser(borrowUser);
		put("data",list);
		return getData2Result();
	}
	/**
	 * 分别查找已审和未审的借用单
	 * @param status 0：未审核  1：已审核
	 * @return
	 */
	@RequiresRoles("后勤管理员")
	@RequestMapping(value="/findBorrowByEUser")
	@ResponseBody
	public Map<String,Object> findBorrowByEUser(Integer status){
		logger.info("查找"+status+"借用单列表");
		if(!init(status).get(STATUS).equals(SUCCESS))
			return getData2Result();
		String Status=null;
		if(status==0){
			Status="未审核";
		}else{
			Status="已审核通过";
		}
		List<Borrow> list =borrowService.findBorrowForEUser(Status);
		put("data",list);
		return getData2Result();
	}
	/**
	 * 删除借用单
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/delBorrow")
	@ResponseBody
	public Map<String,Object> delBorrow(Integer id){
		logger.info("删除借用单");
		if(!init(id).get(STATUS).equals(SUCCESS))
			return getData2Result();
		borrowService.delBorrow(id);
		return getData2Result();
	}
	/**
	 * 根据id归还设备
	 * @param id
	 * @return
	 */
	@RequiresRoles("后勤管理员")
	@RequestMapping(value="/returnBorrow")
	@ResponseBody
	public Map<String,Object> returnBorrow(Integer id){
		logger.info("归还资产");
		if(!init(id).get(STATUS).equals(SUCCESS))
			return getData2Result();
		Borrow borrow = borrowService.findUniqueBorrow(id);
		borrow.setProcess(borrow.YESRETURN);
		borrowService.updateBorrow(borrow);
		//归还设备后资产状况恢复正常
		MainInstrument mainInstrument =instrumentService.findUniqueInstrument(borrow.getMainInstrument().getId());
		mainInstrument.setStatusQuo(mainInstrument.GOOD);
		mainInstrument.setRecipientsPerson(null);
		instrumentService.updateInstrument(mainInstrument);
		return getData2Result();
	}
}
